import pygame
from typing import Tuple, Callable

from game.container import Variables,CONSTANT



class UI:  # abstract format of menus
    def __init__(
            self,
            groups: Tuple[pygame.sprite.LayeredUpdates, ...],
            display_surface = Variables.screen
    ):
        self.groups = groups
        self.component_list = []
        self.display_surface = display_surface
        self.is_components_visible:bool = False
    def on_click(self, key_pos: Tuple[int, int]):
        for group in self.groups:
            for sprite in group:
                if sprite.rect.collidepoint(key_pos):
                    sprite.on_click()




    def draw(self) -> None:  # draw all the sprite in groups
        for group in self.groups:
            for sprite in group:
                sprite.draw()

    def undraw(self) -> None:  # undraw all the sprite in groups
        for group in self.groups:
            for sprite in group:
                sprite.undraw()

    def set_visible(self):
        for group in self.groups:
            for sprite in group:
                sprite.set_visible()
    def update(self):
        self.draw()  # draw all the sprites in self.groups

    def jumpto(self, target_ui, undraw_bg = True):
        if not undraw_bg:   # maybe the current background need to be retained
            self.background.add(target_ui.groups)
            target_ui.groups[0].move_to_back(self.background)
        Variables.last_ui = Variables.current_ui
        Variables.current_ui = target_ui # type: ignore
        self.undraw()
        self.hide_components()
        target_ui.set_visible()
        target_ui.display_components()

    def hide_components(self):
        if len(self.component_list):
            for component in self.component_list:
                component.undraw()
    def display_components(self):
        if len(self.component_list):
            for component in self.component_list:
                component.draw()



